#ifndef TEXTVIEW_H
#define TEXTVIEW_H

#include <QLabel>
#include <QString>

#define GetBMPPATH(bmp) QString(":/image/%1.png").arg(bmp)

typedef struct tagTextViewInfo
{
	QRect crect; //geometry
	QString text;
	QString backgroundBmp;
	QRect rectBmp;
	QRect rectTxt;
	int   fontSize;
    int	  fontWeight;  // 100 ~ 900
	QColor txtColor;
	QColor backgroundColor;
	int flag;
	int index;
	bool bCheckTxtLen;
public:
	tagTextViewInfo () {
		revert();
	}

	void revert() {
        fontSize = 12;
        fontWeight = 60;
		rectBmp.setRect(0, 0, 0, 0);
		rectTxt.setRect(0, 0, 0, 0);
		txtColor = QColor(0, 0, 0);	
        backgroundColor = QColor(0, 0, 0, 0);  //默认透明色
		flag = Qt::AlignCenter|Qt::TextWordWrap; //默认换行
		text.clear();
		backgroundBmp.clear();	
		index = -1;
		bCheckTxtLen = true; //为true时检查text是否能显示完整
	}

} TEXTVIEW_INFO;

QString modifyDrawText(QPainter& painter, const QRect &rcTxt, int flags, QString &text);

class TextView : public QLabel
{
	Q_OBJECT

public:
	TextView(QWidget *parent = 0);
	~TextView();

	void setUI(TEXTVIEW_INFO& ui);
	TEXTVIEW_INFO& getUI() { return mUI; }
    void setBackgroundBmp(const QString& backgroundBmp);

	void setExternalBmp(QString& bmpFile);
	QString getText()
	{
		if (mUI.text.size() <= 0)
		{
			return QLabel::text();
		}
		else
			return mUI.text;
	}
	void setTextEx(const QString& text, int flag = Qt::AlignCenter);

	void setContinueMouseEvent(bool bContinue)  {mbContinueMouseEvent = bContinue;}
	void setCheckTxtLen(bool bCheck) 
	{
		mUI.bCheckTxtLen = bCheck;
	}

	void setHint(const QString& text)
	{
		mHintText = text;
	}
	void setPwMode(bool bPw)
	{
		mbPassWard = bPw;
	}
public Q_SLOTS:
	void setText(const QString& text) {
		mUI.text = text;
		mIsTxtChanged = true;
		update();
	}
	void setTextColor(const QColor& txtColor) {	mUI.txtColor = txtColor; }

public slots:
	void sethovered(bool hovered);
	
protected:
	virtual void paintEvent(QPaintEvent *);
	virtual void mousePressEvent(QMouseEvent * e);
signals:
	void clicked();
	void send(int index);
protected:
	bool mIsTxtChanged;
	TEXTVIEW_INFO mUI;
	QPixmap mPixmap;
private:
	bool mbPassWard;
	QString mTxtToDraw;
	QString mHintText;
	bool mHovered;
	bool mbContinueMouseEvent; //mbContinueMouseEvent为true时，将mousePressEvent传递给其父窗体
};

#endif // TEXTVIEW_H
